﻿using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Read.API.Command.RoleCommand;
using MediatR;

namespace B.S.BaseData.Read.API.CommandHandler.RoleHandler
{
    public class GetDepIdUserCommandHandler : IRequestHandler<GetDepIdUserCommand, APIResult<List<User>>>
    {
        private readonly IBaseRepository<User> userRepos;

        public GetDepIdUserCommandHandler(IBaseRepository<User> userRepos)
        {
            this.userRepos = userRepos;
        }


        public Task<APIResult<List<User>>> Handle(GetDepIdUserCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<User>> result = new APIResult<List<User>>();
            var list = userRepos.GetAll().Where(x => request.DepId.Contains(x.DepartmentId.ToString()));
            if (list != null)
            {
                result.Code = APIEnum.Success;
                result.Msg = "查询成功";
                result.Data = list.ToList();
            }
            else
            {
                result.Code = APIEnum.Failed;
                result.Msg = "查询失败";
            }
            return Task.FromResult(result);
        }
    }
}
